<%= form_for @role do |f| %>
  <%= base_errors_for @role %>

  <ul class="nav nav-tabs" data-tabs="tabs">
    <li class="active"><a href="#primary" data-toggle="tab"><%= _("Role") %></a></li>
    <% if User.current.allowed_to?(:view_filters) %>
      <li><a href="#filters" data-toggle="tab"><%= _('Filters') %></a></li>
    <% end %>
  </ul>

  <div class="tab-content">
    <div class="tab-pane active" id="primary">
      <% if @role.persisted? && (show_location_tab? || show_organization_tab?) %>
        <h5><%= _("Changes to organizations and locations will propagate to all inheriting filters") %></h5>
        <% divider = '<hr>'.html_safe %>
      <% end %>

      <% if @role.cloned_from_id.present? && User.current.can?(:view_roles, @role.cloned_from) %>
        <h5><%= _("This role has been cloned from role %s").html_safe % link_to(@role.cloned_from.name, edit_role_path(@role.cloned_from)) %></h5>
        <% divider = '<hr>'.html_safe %>
      <% end %>

      <%= divider %>

      <%= text_f f, :name, :class => @role.builtin? ? "disabled" : ""  %>
      <%= textarea_f f, :description, :rows=> 5, :size => "col-md-4" %>

      <%= hidden_field_tag :original_role_id, @original_role.id if @cloned_role %>

      <% tax_help = N_("When the role's associated %{taxonomies} are changed,<br> the change will propagate to all inheriting filters.
                         Filters that are set to override <br> will remain untouched. Overriding of role filters can be easily disabled by <br> pressing the \"Disable overriding\" button.
                         Note that not all filters support <br>%{taxonomies}, so these always remain global.") %>
      <% if show_location_tab? %>
        <% loc_help = _(tax_help) % { :taxonomies => _('locations') }%>
        <%= multiple_checkboxes f, :locations, f.object, User.current.my_locations, { :label => _("Locations"), :label_help => loc_help, :label_help_options => { :title => _("Role locations") } } %>
      <% end %>

      <% if show_organization_tab? %>
        <% org_help = _(tax_help) % { :taxonomies => _('organizations') }%>
        <%= multiple_checkboxes f, :organizations, f.object, User.current.my_organizations, { :label => _("Organizations"), :label_help => org_help, :label_help_options => { :title => _("Role organizations") } } %>
      <% end %>
    </div>

    <div class="tab-pane" id="filters">
      <% if @role.new_record? %>
        <%= alert :header => '', :class => 'alert-warning', :close => false,
                  :text => _("Please save the role first. You can edit it later to add filters") %>
      <% else %>
        <div data-ajax-url="<%= filters_path(:role_id => @role, :paginate => 'client') %> table" data-on-complete="tfm.tools.activateDatatables">
          <%= spinner(_('Loading filters ...')) %>
        </div>
        <hr>
        <%= link_to_if_authorized(_("New filter"), hash_for_new_filter_path(:role_id => @role),
                                  { :class => 'btn btn-success pull-right'} ) %>
        <%= link_to_if_authorized(_('Disable all filters overriding'), hash_for_disable_filters_overriding_role_path(:id => @role),
                                  :method => :patch, :class => 'btn btn-default pull-right') %>
      <% end %>
    </div>

    <%= submit_or_cancel f %>
  </div>
<% end %>
